#include <vector>
#include <algorithm>

#include "agentDelete.h"

using namespace std;

// Class used to sort the solutions based on fitness
class SolutionSorter
{
public:
    bool operator()(const pair<double,int>& lh,const pair<double,int>& rh)
    {
        return lh.first < rh.first;
    }
};


void agentDelete::run()
{
    vector<pair<double,int> > theSolutions;

    for (int i = 0; i < thePool->SP_MAX_CAPACITY; ++i)
    {
        solution * sol = thePool->getNthSolution(i);
        if (sol != NULL) theSolutions.push_back(make_pair(sol->getTrainFitness(),i));
    }

    sort(theSolutions.begin(), theSolutions.end(), SolutionSorter());

    for (int x = thePool->TARGET_SIZE; x < theSolutions.size(); x++)
    {
        solution* sol = thePool->getNthSolution(theSolutions[x].second);
        if (sol != thePool->getBestSolution())
        {
            thePool->setNthSolution(theSolutions[x].second,NULL);
            delete(sol);
        }
    }
}
